package com.soooft.tickey.security;

import com.soooft.tickey.po.User;
import com.soooft.tickey.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;


/**
 * @description
 * @author: Fanxl
 * @date: Created in 2017/9/20 13:18
 */
@Slf4j
@Component
public class MyUserDetailsService implements UserDetailsService, SocialUserDetailsService {

    @Autowired
    private UserService userService;

//    @Autowired
//    private EmployeeService employeeService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户:"+username);
        //根据用户名查找用户信息
        //根据查找到的用户信息判断用户是否被冻结
//        Operator user = null;
//        if (StringUtils.startsWith(username, SmsCodeAuthenticationFilter.FAN_FORM_MOBILE_KEY)) {
//
//        }else {
//            user = operatorService.getByCode(username);
//        }
//        if (user==null){
//            throw new UsernameNotFoundException("用户名错误");
//        }
//        String accountSuite = DbContextHolder.getCurrentAccountSuit();
//        log.info("用户登录设置账套:"+accountSuite);
//        user.setAccountSuite(accountSuite);
//        user.setPassword(user.getPassword()+","+user.getCode());
        return null;
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        log.info("Social认证登录:" + userId);
        String[] data = userId.split(":");
        String accountSuite = data[0];
        String realUserId = data[1];
//        DbContextHolder.setCurrentAccountSuit(accountSuite);
//
        User user = userService.getById(realUserId);
//        if (user==null){
//            Employee employee = employeeService.getById(realUserId);
//            if (employee!=null){
//                if (employee.getOperatorId()!=null){
//                    user = operatorService.getById(employee.getOperatorId());
//                }else {
//                    user = operatorService.getByCode("admin");
//                }
//            }
//        }
//        user.setAccountSuite(accountSuite);
//        user.setPassword(user.getPassword());
        return null;
    }
}